package org.wlgzs.shop.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wlgzs.shop.bean.Msg;
import org.wlgzs.shop.bean.ShowDate;
import org.wlgzs.shop.bean.Store;
import org.wlgzs.shop.bean.StoreExample;
import org.wlgzs.shop.dao.StoreMapper;
import org.wlgzs.shop.service.IStoreService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service(value = "StroeService")
public class StroeServiceImpl implements IStoreService {

    @Autowired
    StoreMapper storeMapper;


    private static final Logger logger = LoggerFactory.getLogger(StroeServiceImpl.class);

//所有店铺
    @Override
    public List<Store> allStore() {
        return storeMapper.selectByExample(null);
    }

    /**
     * 根据店铺信息模糊查询
     *
     * @param storeName
     * @return List<Store>
     */

    @Override
    public List<Store> StoreNameSelectStore(String storeName) {
        StoreExample storeExample = new StoreExample();
        StoreExample.Criteria criteria = storeExample.createCriteria();
        criteria.andStoreNameLike("%" + storeName + "%");
        List<Store> storeList = storeMapper.selectByExample(storeExample);
        return storeList;
    }


    //创建店铺
    @Override
    public Msg buildStore(Store store) {
        storeMapper.insert(store);
        return Msg.success();
    }

    //修改店铺名称
    @Override
    public Msg setStoreName(int storeId, String reName) {
        //storeMapper.updateByPrimaryKeySelective()
        return null;
    }

    //获取所有店铺
    public List<Store> list() {
        StoreExample storeExample = new StoreExample();
        storeExample.createCriteria();
        return storeMapper.selectByExample(null);
    }

    //获取storeId和图片路径
    @Override
    public List<ShowDate> storeDate(List<Store> dateList,int number) {
        List<ShowDate> list = new ArrayList<ShowDate>();
        String[] date;
        int k=0;
        if(number>dateList.size()){
            number=dateList.size();
        }
        for (Store store : dateList) {
            k++;
            ShowDate showDate=new ShowDate();
            showDate.setName(store.getStoreName());
            showDate.setId(store.getStoreId());
            date=store.getPhotoUrl().split(",");
            showDate.setPhotoName(date[0]);
            list.add(showDate);
            if(k>=number){
                break;
            }
        }
        return list;
    }


    //根据分类查询店铺
    @Override
    public List<ShowDate> selectStore(int storeKindId){
        StoreExample storeExample=new StoreExample();
        StoreExample.Criteria criteria=storeExample.createCriteria();
        criteria.andStoreKindEqualTo(storeKindId+"");
        storeMapper.selectByExample(storeExample);
        return storeDate(storeMapper.selectByExample(storeExample),storeMapper.selectByExample(storeExample).size());
    }

    @Override
    public List<Store> selectStoreById(int id) {
        StoreExample storeExample=new StoreExample();
        StoreExample.Criteria criteria=storeExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        return storeMapper.selectByExample(storeExample);
    }

    @Override
    public List<Store> selectStoreLikeDateByStireId(String date,int userId) {
        StoreExample storeExample=new StoreExample();
        StoreExample.Criteria criteria=storeExample.createCriteria();
        System.out.println(userId);
        criteria.andUserIdEqualTo(userId);
        storeExample.or().andStoreNameLike("%"+date+"%");
        return storeMapper.selectByExample(storeExample);
    }


}
